package algorithm.leetcode

import java.math.BigDecimal
import java.util.*

object ReverseBits {
//12 - 3
    /**
     * 0110 1110 1111
     * 0, 3, 7
     * 10001
     * 111100
     */
    fun reverseBits(num: Int): Int {
        var bins = num.toString(2)
        val mod = bins.length % 4
        if(mod != 0){
            repeat(4-mod){
                bins = "0$bins"
            }
        }
        println("bins = $bins")
        val idx = mutableListOf<Int>()
        for (i in bins.indices){
            val c = bins[i]
            if(c == '0'){
                idx.add(i)
            }
        }

        var maxlen = 0
        if(idx.size==1){
            return bins.length
        }else if(idx.size == 0){
            return 0
        }else{
            for (i in 0 until idx.size){
                if(i == 0){
                    maxlen = maxlen.coerceAtLeast(idx[i+1])
                }else if(i == idx.size-1){
                    maxlen = maxlen.coerceAtLeast(bins.length-1 - (idx[i-1]+1) + 1)
                }else{
                    maxlen = maxlen.coerceAtLeast(idx[i+1]-1 - (idx[i-1]+1) + 1)
                }
            }
        }
        return maxlen
    }
}

fun main() {
    val bd = BigDecimal("10")

    println(bd.toBigInteger().toString(2))
//    println(ReverseBits.reverseBits(-1))
}